package com.fcgzs.member.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.fcgzs.common.utils.JWTUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fcgzs.member.entity.MemberEntity;
import com.fcgzs.member.service.MemberService;
import com.fcgzs.common.utils.PageUtils;
import com.fcgzs.common.utils.R;



/**
 * 会员-会员表
 *
 * @author ljj
 * @email ljj@gmail.com
 * @date 2021-08-19 11:36:38
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    //private RedisTemplate redisTemplate;

    @Autowired     //注意：  StringRedisTemplate保存的数据与RedisTemplate保存的数据不互通
    private StringRedisTemplate redisTemplate;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/countByDateTime")
    public R countByDateTime(String beginTime,String endTime){
        List<Map<String, Object>> mapList = memberService.countByDateTime(beginTime, endTime);
        return R.ok().put("mapList",mapList);
    }

    @RequestMapping("/login")
    public R login(String username,String password){
        //1.判断用户是否登录成功
        MemberEntity memberEntity = memberService.login(username, password);
        if(memberEntity==null){
            return R.error("用户名或密码不匹配");
        }

        //2.如果登录成功则生成JWT令牌
        String token = JWTUtil.createJWT(username);

        //3.将令牌存入到缓存中
        String key = UUID.randomUUID().toString().replace("-", "");

        redisTemplate.opsForHash().put(key,"token",token);
        redisTemplate.opsForHash().put(key,"username",username);
        //设置缓存中令牌的有效期
        redisTemplate.expire(key,JWTUtil.TOKEN_EXPIRE_TIME, TimeUnit.MILLISECONDS);
        return R.ok().put("token",token).put("refreshToken",key);
    }


    @RequestMapping("/refreshToken")
    public R refreshToken(String refreshToken){
        //1.通过key值在缓存中获取用户信息
       String username = (String) redisTemplate.boundHashOps(refreshToken).get("username");

        if(StringUtils.isNotEmpty(username)){
            //2.如果有用户信息，则重新生成令牌
            String token = JWTUtil.createJWT(username);
            //3.将新的令牌放入缓存
            redisTemplate.opsForHash().put(refreshToken,"token",token);
            redisTemplate.opsForHash().put(refreshToken,"username",username);
            return R.ok().put("token",token).put("refreshToken",refreshToken);
        }else{
            return R.error("该令牌以失效");
        }

    }

}
